const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpackBaseConfig = require("./webpack.common");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const merge = require("webpack-merge");
const utils = require("./utils");

class ChunksFromEntryPlugin {
  apply(compiler) {
    compiler.hooks.emit.tap("ChunksFromEntryPlugin", (compilation) => {
      compilation.hooks.htmlWebpackPluginAlterChunks.tap(
        "ChunksFromEntryPlugin",
        (_, { plugin }) => {
          const entry = plugin.options.entry;
          const entrypoint = compilation.entrypoints.get(entry);
          return entrypoint.chunks.map((chunk) => ({
            names: chunk.name ? [chunk.name] : [],
            files: chunk.files.slice(),
            size: chunk.modulesSize(),
            hash: chunk.hash,
          }));
        }
      );
    });
  }
}

let prodWebpackConfig = {
  mode: "production",
  devtool: "cheap-module-source-map",
  optimization: {
    minimizer: [new OptimizeCSSAssetsPlugin({})],
  },
  plugins: [
    new UglifyJsPlugin({
      sourceMap: true,
      parallel: true,
    }),
    new MiniCssExtractPlugin({
      filename: "static/css/[name].[hash:7].css",
    }),
  ],
  optimization: {
    splitChunks: {
      chunks: "all",
      minSize: 30000,
      minChunks: 1,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: "~",
      name: true,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true,
        },
      },
    },
  },
};

prodWebpackConfig = utils.pushHtmlWebpackPlugins(
  merge(webpackBaseConfig, prodWebpackConfig),
  {
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeAttributeQuotes: true,
    },
  }
);

prodWebpackConfig.plugins.push(new ChunksFromEntryPlugin());
module.exports = prodWebpackConfig;
